package com.telecom.weixi.view;

import com.telecom.weixi.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EditWithLabelView extends LinearLayout {

	private TextView mLabel;
	private EditText mContent;
	
	public EditWithLabelView(Context context){
		this(context, null);
		setupView(context);
	}
	
	public EditWithLabelView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setupView(context);
		
		if(attrs != null){
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LabelEdit);
			String label = a.getString(R.styleable.LabelEdit_label);
			String hint = a.getString(R.styleable.LabelEdit_hint);
			float gap = a.getDimension(R.styleable.LabelEdit_gap, 3.0f);
			boolean isPasswd = a.getBoolean(R.styleable.LabelEdit_passwd, false);
			setLabelGap((int)gap);
			setLabel(label);
			if(hint != null){
				setHint(hint);
			}
			if(isPasswd){
				mContent.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
		}
	}
	
	private void setupView(Context context){
		LayoutInflater flater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		flater.inflate(R.layout.label_edit, this);
		
		mLabel = (TextView) findViewById(R.id.label);
		mContent = (EditText) findViewById(R.id.content);
		
		this.setOnClickListener(focuseListener);
	}
	
	View.OnClickListener focuseListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			mContent.requestFocus();
		}
	};
	
	public String getText(){
		return mContent.getText().toString();
	}
	
	public void setText(String content){
		mContent.setText(content);
	}
	
	public String getLabel(){
		return mLabel.getText().toString();
	}
	
	public void setLabel(String label){
		mLabel.setText(label);
	}
	
	public void setHint(String hint){
		mContent.setHint(hint);
	}
	
	public void setLabelGap(int gap){
		mContent.setPadding(gap, 0, 0, 0);
	}
}
